/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * UserClientManager.java
 * netsystem
 * 2014年3月1日下午2:50:54
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.User;
import org.jackysoft.entity.nosql.PrivateChatPair;

import com.corundumstudio.socketio.SocketIOClient;
import com.google.common.collect.Sets;

public final  class OnlineUserManager extends ConcurrentHashMap<SocketIOClient, User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8239515919256780269L;
	private static final Map<User,SocketIOClient> innerMap = new ConcurrentHashMap<>();
	//HashMap<User,SocketIOClient>();
	private static final Set<PrivateChatPair> chatPair = new HashSet<>();
	private static final Set<String> privateChat = new HashSet<>();
	private static final Set<String> publicRoom = new HashSet<>();
	static final Log logger = LogFactory.getLog(OnlineUserManager.class);

	@Override
	public User put(SocketIOClient key, User value) {
		innerMap.put(value, key);
		return super.put(key, value);
	}

	@Override
	public User remove(Object key) {
		User ukey = super.remove(key);
		innerMap.remove(ukey);
		return ukey;
	}
	
	public Collection<SocketIOClient> getClients(){
		return this.keySet();
	}
	public Collection<User>  getUsers(){
		return this.values();
	}
	
	public SocketIOClient clientOf(User user){
		return innerMap.get(user);
	}
	
	public SocketIOClient clientOf(String user){
		User temp = new User(user);		
		SocketIOClient sco = innerMap.get(temp);		
		return sco;
	}	
	public boolean hasLogin(User user){
	    return clientOf(user)!=null;
	}
	
	public boolean hasLogin(String user){
		return clientOf(user)!=null;
	}
	
	/**
	 * 加入私聊
	 * */
	public boolean addChat(PrivateChatPair chat){
		if(chat==null) return false;
		privateChat.add(chat.getSender().getName());
		privateChat.add(chat.getReceiver().getName());
		return chatPair.add(chat);
	}
	
	/**
	 * 离开私聊
	 * */
	public void removeChat(String name){
		Set<PrivateChatPair> t = Sets.newHashSet();
		for(PrivateChatPair pt:chatPair){
			if(pt.getSender().getName().equals(name) ||
				pt.getReceiver().getName().equals(name)	
					){
				t.add(pt);
			}
				
		}		
		chatPair.removeAll(t);
	}
	
	
	public boolean joinPublicRoom(String user){
		return publicRoom.add(user );
		
	}
	
	public boolean leavePublicRoom(String user){
		return publicRoom.remove(user);
	}
	
	public boolean inPublic(String user){
		return publicRoom.contains(user);
	}
	
	public void trace(){
		logger.info("start trace online user\n\n");
		for(Map.Entry<SocketIOClient, User> entry :this.entrySet()){
			logger.info(entry.getKey().getSessionId()+"-->"+entry.getValue().getUsername());
		}

		logger.info("\n\nend trace online user");
	}

}
